
import { _decorator, Component, Event, Node, Vec2, Vec3 } from 'cc';
import { MonsterManager } from '../monster/MonsterManager';
import { Tools } from '../../tools/Tools';
import EvtManger from '../../tools/EvtManger';
import { EventName } from '../../manager/AppControll';
import { GameData } from '../../system/GameData';
import { Hp } from './Hp';
import { MoveType } from '../../tools/typings';
const { ccclass, property } = _decorator;


@ccclass('Move')
export class Move extends Component {
    private mV2StartPoint: Vec2 = null;
    private mV2EndPoint: Vec2 = null
    private mSpeed: number = null;
    private mStart: boolean = false;
    private mCurDistance = 0;
    private mIsFinish = false;

    private mLtPoint: number[][] = [];
    private mIndex = 0;
    // readonly x: number;
    // readonly y: number;

    start() {
    }


    InitPointLt(lt: number[][], speed: number) {
        this.mIsFinish = false;
        this.mCurDistance = 0
        this.mSpeed = speed;
        this.mLtPoint = lt;
        this.mIndex = 0;
        this.UpdateSEPoint();
        this.node.position = new Vec3(this.mV2StartPoint.x, this.mV2StartPoint.y, 0);
        this.mStart = true;
    }

    UpdateSEPoint() {
        this.mV2StartPoint = new Vec2(this.mLtPoint[this.mIndex][0], this.mLtPoint[this.mIndex][1]);
        this.mV2EndPoint = new Vec2(this.mLtPoint[this.mIndex + 1][0], this.mLtPoint[this.mIndex + 1][1]);

        this.node.position = new Vec3(this.mV2StartPoint.x, this.mV2StartPoint.y, 0);
        if (this.mV2EndPoint.x > this.mV2StartPoint.x) {
            this.node.scale = new Vec3(-1, this.node.scale.y, this.node.scale.z)
        } else {
            this.node.scale = new Vec3(1, this.node.scale.y, this.node.scale.z)
        }
        this.scheduleOnce(() => {
            if (this.node.getComponent(Hp)) this.node.getComponent(Hp).ChangeDirection();
        })
    }

    InitPoint(pointS: Vec2, pointE: Vec2, mSpeed: number) {
        this.mIsFinish = false;
        let lt = [[pointS.x, pointS.y], [pointE.x, pointE.y]]
        this.InitPointLt(lt, mSpeed);
        lt = null;
    }

    Reverse(mSpeed: number) {
        this.mCurDistance = 0;
        let temp = this.mV2StartPoint.clone();
        this.mV2StartPoint = this.mV2EndPoint;
        this.mV2EndPoint = temp;
        this.mStart = true;
        this.mIsFinish = false;
        this.mSpeed = mSpeed;
        this.node.position = new Vec3(this.mV2StartPoint.x, this.mV2StartPoint.y, 0);
        this.node.scale = new Vec3(this.node.scale.x * -1, this.node.scale.y, this.node.scale.z)
        this.scheduleOnce(() => {
            if (this.node.getComponent(Hp)) this.node.getComponent(Hp).ChangeDirection();
        })
    }

    get Speed() {
        return this.mSpeed;
    }

    set Speed(value) {
        this.mSpeed = value;
    }

    update(deltaTime: number) {
        
        if (GameData.isPause) return;


        if (!this.mStart || this.mIsFinish == true) return;

        let totalDistance = this.mV2StartPoint.clone().subtract(this.mV2EndPoint).length();
        let curDistance = deltaTime * this.mSpeed;
        this.mCurDistance += curDistance;
        let direction = this.mV2EndPoint.clone().subtract(this.mV2StartPoint).normalize(); //计算出距离终点的距离
        let currentPosition = this.mV2StartPoint.clone().add(direction.multiplyScalar(this.mCurDistance));

        this.node.setPosition(new Vec3(currentPosition.x, currentPosition.y, 0));
        if (this.mCurDistance >= totalDistance && this.mIndex >= this.mLtPoint.length - 2) {
            this.mIsFinish = true;
            this.mSpeed = 0;
            this.node.emit("MoveFinish");
        } else if (this.mCurDistance >= totalDistance && this.mIndex < this.mLtPoint.length - 2) {
            this.mCurDistance = 0;
            this.mIndex++;
            this.UpdateSEPoint();
        }

        currentPosition = null;
        direction = null;

    }

    GetPointByTime(dt): Vec2 {
        let totalDistance = this.mV2StartPoint.clone().subtract(this.mV2EndPoint).length();
        let curDistance = dt * this.mSpeed;
        let direction = this.mV2EndPoint.clone().subtract(this.mV2StartPoint).normalize();
        let currentPosition = this.mV2StartPoint.clone().add(direction.multiplyScalar(this.mCurDistance + curDistance));
        if (curDistance >= totalDistance) {
            return this.mV2EndPoint.clone();
        } else {
            return new Vec2(currentPosition.x, currentPosition.y);
        }
    }

    GetDirection(): Vec2 {
        return this.mV2EndPoint.clone().subtract(this.mV2StartPoint).normalize();
    }

    get StartPoint() {
        return this.mV2StartPoint;
    }
}

